package com.test.webflux.controller;

import com.test.webflux.entity.Product;
import com.test.webflux.entity.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;

/**
 * TODO
 *
 * @Description
 * @Author lingmin
 * @Date 2025/4/14 14:16
 **/
@Slf4j
//@RequestMapping("/product")
//@RestController
public class ProductController {


    @GetMapping(value = "/title")
    public Mono<String> getTitle() {
        log.info("title");
        return Mono.just("产品信息");
    }


    @PostMapping(value = "/list" , produces = MediaType.APPLICATION_JSON_VALUE)
    public Mono<ResponseResult<Product>> list(Product product) {
        log.info("list.product:{}" , product);
        ResponseResult<Product> result = new ResponseResult();
        result.setCode("000000");
        result.setMsg("成功");
        result.setData(product);
        return Mono.just(result);
    }


    @PostMapping(value = "/save" , produces = MediaType.APPLICATION_JSON_VALUE)
    public Mono<ResponseResult<Product>> save(Mono<Product> mono) {
        //请求需要返回的处理方式
        return mono.flatMap(product -> {
            log.info("save.product:{}" , product);
            ResponseResult<Product> result = new ResponseResult();
            result.setCode("000000");
            result.setMsg("处理成功");
            result.setData(product);
            return Mono.just(result);
        });
    }

    @PostMapping(value = "/subscribe" , produces = MediaType.APPLICATION_JSON_VALUE)
    public Mono<Void> subscribe(Mono<Product> mono) {
        log.info("subscribe");
        //无返回处理
         mono.subscribe(product -> {
             //TODO 此处未执行
            log.info("subscribe.product:{}" , product);
            log.info("订阅处理");
        });
        //mono.publish()
         return Mono.empty();
    }



}
